<template>
    <el-dialog
        title="更新质控点状态"
        :close-on-click-modal="false"
        :visible.sync="dialogParams.visible"
        width="700px"
    >
        <el-form
            ref="formData"
            :rules="rules"
            :model="formData"
            label-width="100px"
        >
            <el-form-item v-if="isSSWGH" label="起始状态" prop="startValue">
                <el-input
                    type="text"
                    placeholder="请输入起始值"
                    v-model.trim="formData.startValue"
                    @change="valChange"
                ></el-input>
            </el-form-item>
            <el-form-item label="更新校准值" prop="val">
                <el-input
                    type="text"
                    placeholder="请输入校准值"
                    v-model.trim="formData.val"
                    @change="valChange"
                ></el-input>
            </el-form-item>
            <el-form-item v-if="isSSWGH" label="累计值" prop="cumulativeValue">
                <el-input
                    type="text"
                    placeholder="自动计算累计值"
                    readonly
                    v-model.trim="formData.cumulativeValue"
                ></el-input>
            </el-form-item>
            <!-- 5月16注释 -->
            <!--  <el-form-item label="备注">
        <el-input type="textarea" :autosize="{ minRows: 6, maxRows: 8}" placeholder="请输入内容">
        </el-input>
      </el-form-item> -->
            <el-form-item label="选择文件" prop="filePath">
              <el-upload
                    ref="upload"
                    class="upload-demo"
                    drag
                    :action="`${baseURL}/v1/pc/api/file/fileUpload`"
                    :on-change="handleChange"
                    :on-remove="fileRemove"
                    :on-exceed="onExceed"
                    multiple
                    :limit="5"
                    :auto-upload="false"
                    :headers="aa"
                    :file-list="fileList"
                    :data="uploadPrams"
                >
                    <i class="el-icon-upload"></i>
                    <div class="el-upload__text">
                        将文件拖到此处，或<em>点击上传</em>
                    </div>
                    <!--<div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>-->
                </el-upload>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
                <el-input
                    type="textarea"
                    :autosize="{ minRows: 2, maxRows: 4 }"
                    placeholder="请输入内容"
                    v-model.trim="formData.remark"
                ></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close('formData')">关 闭</el-button>
            <el-button type="primary" @click="submit('formData')"
                >保 存</el-button
            >
        </span>
    </el-dialog>
</template>

<script>
import { addData, updateCorrectValue } from '@/api/target';
import { fileUpload } from '@/api/improve';

export default {
    name: 'updateCorrectVal',
    props: {
        dialogParams: {
            type: Object
        }
    },
    data () {
        return {
            aa: {
                Accept: 'application/json',
                'X-Requested-With': 'XMLHttpRequest'
            },
            formData: {
                startValue: '',
                val: '',
                cumulativeValue: '',
                remark: '',
                fileName: ''
                // targetDesc: ''
            },
            rules: {
                startValue: [
                    { required: true, message: '请输入起始值', trigger: 'blur' }
                ],
                val: [
                    { required: true, message: '请输入校准值', trigger: 'blur' }
                ],
                fileName: [
                    {
                        required: true,
                        message: '请至少选择一项文件进行上传',
                        trigger: 'change'
                    }
                ]
            },
            baseURL: this.baseURL,
            isSSWGH: false,
            uploadPrams: {
                busType: 'level',
                fileType: 'file'
            }, // 上传图片所需参数
            fileList: [], // 回传的文件信息
            fileForm: {}
        };
    },
    created () {
        this.formData.startValue = this.dialogParams.startValue;
        this.formData.val = this.dialogParams.correctValue;
        this.formData.cumulativeValue = this.dialogParams.cumulativeValue;
        this.isSSWGH = this.dialogParams.chargeCategory === 'XT_ZKDLB_SSWGH';
        console.log(this.dialogParams);
    },
    methods: {
        submit (formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    if (this.formData.cumulativeValue === 'null') {
                        this.formData.cumulativeValue = null;
                    }

                    if (this.formData.startValue === 'null') {
                        this.formData.startValue = null;
                    }
                    const data = {
                        id: this.dialogParams.id,
                        startValue: this.formData.startValue,
                        correctValue: this.formData.val,
                        cumulativeValue: this.formData.cumulativeValue,
                        proveFilePath: this.fileForm.filePath
                    };
                    // console.log(data)
                    const formData = new FormData();
                    this.formData.fileName.forEach(file => {
                        // console.log(file);
                        formData.append('files', file.raw);
                    });
                    formData.append('busType', this.uploadPrams.busType);
                    formData.append('fileType', this.uploadPrams.fileType);
                    formData.append('remark', this.formData.remark);
                    formData.append('materialId', this.dialogParams.id);
                    console.log(formData, 'formList', this.formData.fileName);
                    fileUpload(formData).then(res => {
                        // console.log(res, 'res');
                        if (res) {
                          this.message('success', '上传成功!');
                            this.dialogParams.visible = false
                        }
                    });
                    /* updateCorrectValue(data)
                        .then(res => {
                            this.$message.success('更新成功！');
                            this.dialogParams.visible = false;
                            this.$emit('getList');
                        }) */
                } else {
                    return false;
                }
            });
        },
        // 关闭
        close (formName) {
            this.$refs[formName].resetFields();
            this.dialogParams.visible = false;
        },
        // 上传文件
        uploadFile () {
            this.$refs.fileForm.validate(valid => {
                if (valid) {
                    const params = {
                        ...this.fileForm,
                        busType: 'level',
                        fileType: 'file'
                    };
                    addData(params)
                        .then(res => {
                            this.$message.success('操作成功');
                            this.$emit('getList');
                            this.uploadVisible.visible = false;
                        })
                        .catch();
                }
            });
        },
        valChange () {
            if (this.isSSWGH) {
                try {
                    this.formData.cumulativeValue =
                        parseFloat(this.formData.startValue) +
                        parseFloat(this.formData.val);
                } catch (e) {
                    this.$message.warning('输入值无法累计！');
                }
            }
        },
        handleSuccess (res, file) {
            if (res.code === 'SUCCESS') {
                this.fileForm.filePath = res.data;
            }
        },
        handleChange (file, fileList) {
            console.log(fileList, 'fileList');
            fileList.length >= 1 && (this.formData.fileName = fileList);
        },
        handleBeforeUpload (file) {
            const data = {};
            data.busType = 'level';
            data.fileType = 'file';
            this.uploadPrams = data;
            return Promise.resolve(file);
        },
        fileRemove (file, fileList) {
            this.fileList = {};
        },
        onExceed (file, fileList) {
            this.$message.error('请先移除下方已经上传的文件再重新上传！');
        }
    },
    watch: {
        dialogParams (val) {
            console.log(val);
        }
    }
};
</script>

<style scoped></style>
